<?php
/**
 * Created by PhpStorm.
 *
 * Author : Traveler.
 * Time   : 2020/2/21 22:33
 * Company: Inner Mongolia Zhengzhi Network Technology Co., Ltd
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\middleware\Auth;
use app\admin\middleware\Login;
use app\admin\model\IcafeCate as IcafeCateModel;
use think\facade\View;

class IcafeCate extends BaseController
{
    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [
        Auth::class => ['only' => ['add', 'edit', 'status', 'delete']],
        Login::class,
    ];

    /**
     * 接口：获取场所分类(表格渲染)
     * @return \think\response\Json
     */
    public function getTableData()
    {
        $param = $this->request->param();
        $data  = IcafeCateModel::getIcafeCateTable($param);

        return $this->success('获取场所分类成功', $data['data'], ['count' => $data['count']]);
    }

    /**
     * 接口：获取场所分类(下拉选择组件渲染)
     * @return \think\response\Json
     */
    public function getValidIcafeCate()
    {
        return $this->success('获取场所分类列表成功', IcafeCateModel::getValidIcafeCate());
    }

    /**
     * 页面渲染：场所分类管理
     * @return string
     */
    public function index()
    {
        // 权限标识
        $flag = [
            'add'    => 'icafecate:add',
            'edit'   => 'icafecate:edit',
            'status' => 'icafecate:status',
            'delete' => 'icafecate:delete',
        ];

        return View::assign('flag', $flag)->fetch();
    }

    /**
     * 添加场所分类
     * @return string|\think\response\Json
     */
    public function add()
    {
        // 提交
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证数据
            $validate = $this->validate($data, 'IcafeCate');
            if ($validate !== true) {
                return $this->error($validate);
            }

            if (!isset($data['status'])) {
                $data['status'] = 0;
            }

            $result = IcafeCateModel::create($data);
            if (!$result->isEmpty()) {
                return $this->success('添加成功');
            }

            return $this->error('添加失败');
        }

        // 渲染页面：添加场所分类
        return View::fetch();
    }

    /**
     * 编辑场所分类
     * @return string|\think\response\Json
     */
    public function edit()
    {
        // 提交
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证数据
            $validate = $this->validate($data, 'IcafeCate');
            if ($validate !== true) {
                return $this->error($validate);
            }

            if (!isset($data['status'])) {
                $data['status'] = 0;
            }

            $result = IcafeCateModel::update($data);
            if (!$result->isEmpty()) {
                return $this->success('编辑成功');
            }

            return $this->error('编辑失败');
        }

        // 渲染页面：编辑场所分类
        return View::fetch();
    }

    /**
     * 批量启用/禁用
     * @return \think\response\Json
     */
    public function status()
    {
        // 提交
        if ($this->request->isPost()) {
            $status = $this->request->post('status') ?: 0;
            $operation = 0 == $status ? '禁用' : '启用';
            $ids = trim($this->request->post('idsStr'), " \t\n\r\0\x0B,");

            $result = IcafeCateModel::whereIn('id', $ids)->update(['status' => $status]);
            if ($result) {
                return $this->success($operation . '成功：' . $result . '条');
            }

            return $this->error($operation . '失败');
        }

        return $this->error('非法请求');
    }

    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function delete()
    {
        // 提交
        if ($this->request->isPost()) {
            $ids = trim($this->request->post('idsStr'), " \t\n\r\0\x0B,");

//            $result = IcafeCateModel::whereIn('id', $ids)->update(['delete_time' => time()]);
            $result = IcafeCateModel::whereIn('id', $ids)->delete();
            if ($result) {
                return $this->success('删除成功：' . $result . '条');
            }

            return $this->error('删除失败');
        }

        return $this->error('非法请求');
    }
}